<?php

use think\migration\Seeder;
use think\console\Output;

/**
 * 测试数据
 */
class TestData extends Seeder
{
    public function run(): void
    {
        $this->setOutput(new Output);       // 设置 output class

        $timestamps = [
            'create_time' => time(),
            'update_time' => time(),
        ];

        // ===================== 添加超级角色 =====================
        $dispatch = $this->table('shop_dispatch');

        $result = $dispatch->getAdapter()->fetchRow("select count(*) as has from " . $dispatch->getAdapter()->getAdapterTableName('shop_dispatch') . " where id = 1");
        if ($result['has']) {
            // 已经存在运费模板则提示，并跳过
            $this->output->writeln(' ==' . ' <info>' . get_class($this) . ':</info>' . ' <comment>The dispatch already exists.</comment>');
        } else {
            $data = array_merge([
                'id' => 1,
                'name' => '全国包邮',
                'type' => 'express',
                'status' => 'normal',
            ], $timestamps);

            // 初始化超级管理组
            $dispatch->insert($data)->save();

            $dispatchExpress = $this->table('shop_dispatch_express');
            $expressData = array_merge([
                'id' => 1,
                'dispatch_id' => 1,
                'type' => 'number',
                'first_num' => 10,
                'first_price' => '0.00',
                'additional_num' => 0,
                'additional_price' => '0.00',
                'province_ids' => '11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65',
                'city_ids' => '',
                'district_ids' => '',
                'weigh' => 0,
            ], $timestamps);

            // 初始化超级管理组
            $dispatchExpress->insert($expressData)->save();

            $this->output->writeln(' ==' . ' <info>' . get_class($this) . ':</info>' . ' <comment>The dispatch create successed.</comment>');
        }


        $dataConfig = [
            [
                'table' => 'shop_goods',
                'file' => 'TestGoods',
                'field' => 'id',
                'need_time' => true,
                'json' => ['images', 'params']
            ],
            [
                'table' => 'shop_goods_sku',
                'file' => 'TestGoodsSku',
                'field' => 'id',
            ],
            [
                'table' => 'shop_goods_sku_price',
                'file' => 'TestGoodsSkuPrice',
                'field' => 'id',
                'need_time' => true,
            ],
            [
                'table' => 'shop_goods_service',
                'file' => 'TestGoodsService',
                'field' => 'id',
                'need_time' => true,
            ],
            [
                'table' => 'shop_category',
                'file' => 'TestCategory',
                'field' => 'id',
                'need_time' => true,
            ],
            [
                'table' => 'shop_data_faq',
                'file' => 'TestDataFaq',
                'field' => 'id',
                'need_time' => true,
            ],
        ];

        foreach ($dataConfig as $key => $config) {
            $table = $this->table($config['table']);

            $fields = $table->getAdapter()->fetchAll("select " . $config['field'] . " from " . $table->getAdapter()->getAdapterTableName($config['table']));
            $fields = array_column($fields, $config['field']);
            $filePath = root_path() . 'database' . DIRECTORY_SEPARATOR . 'seeds' . DIRECTORY_SEPARATOR . 'test' . DIRECTORY_SEPARATOR . $config['file'] .'.php';

            $datas = include $filePath;
            foreach ($datas as $k => $data) {
                if (!in_array($data[$config['field']], $fields)) {
                    if (isset($config['json'])) {
                        foreach ($config['json'] as $jsonField) {
                            // 特殊字段转json
                            $data[$jsonField] = is_array($data[$jsonField]) ? json_encode($data[$jsonField]) : $data[$jsonField];
                        }
                    }
                    if (isset($config['need_time']) && $config['need_time']) {
                        $data = array_merge($data, $timestamps);
                    }
                    $table->insert($data)->save();
                }
            }
            $this->output->writeln(' ==' . ' <info>' . get_class($this) . ':</info>' . ' <comment>The test data ' . $config['table'] . ' create successed.</comment>');
        }
    }
}
